package com.situ.personal.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

import com.situ.personal.util.JDBCUtil;
import com.situ.personal.util.ModelConvertUtil;

public class UserRoleDao {

	public int getTotalCount() {
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT COUNT(*) FROM `user` AS u INNER JOIN user_role AS ur ON u.id=ur.user_id  INNER JOIN role AS r ON ur.role_id=r.id";
			preparedStatement = connection.prepareStatement(sql);
			System.out.println("总页数SQL/UserRoleDao23Line"+preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				count = resultSet.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(resultSet, preparedStatement, connection);
		}
		return count;
	}

	public List<Map<String, Object>> fandPageInfo(int offset, int pageSize) {
		List<Map<String, Object>> list = null;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT u.id,u.name,u.email,r.id as roleId,r.name AS roleName FROM `user` AS u INNER JOIN user_role AS ur ON u.id=ur.user_id  INNER JOIN role AS r ON ur.role_id=r.id limit ?,?";
			preparedStatement = connection.prepareStatement(sql);
			System.out.println("每页现实的内容SQL/UserDao47Line"+preparedStatement);
			preparedStatement.setInt(1, offset);
			preparedStatement.setInt(2, pageSize);
			resultSet = preparedStatement.executeQuery();
			list = ModelConvertUtil.converToList(resultSet);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(resultSet, preparedStatement, connection);
		}
		return list;
	}// fandPageInfo

	
	public boolean deleteRoleByUserId(int id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		boolean isDeleteRole = false;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from user_role where user_id=?";
			//DELETE FROM user_role WHERE user_id=?
			preparedStatement = connection.prepareStatement(sql);
			System.out.println("是否删除Role/deleteRoleByUserId/UserDao252Line："+preparedStatement);
			preparedStatement.setInt(1,id);
			
			int count = preparedStatement.executeUpdate();
			if (count>0) {
				isDeleteRole = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(resultSet, preparedStatement, connection);
		}
		return isDeleteRole;
	}

	public boolean insert(Integer userId, Integer roleId) {
		boolean isSuccess = false;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO user_role(user_id,role_id) VALUES(?,?)";
			preparedStatement = connection.prepareStatement(sql);
			System.out.println("添加userRole的SQL语句："+preparedStatement);
			preparedStatement.setInt(1,userId);
			preparedStatement.setInt(2,roleId);
			int count = preparedStatement.executeUpdate();
			if (count>0) {
				isSuccess = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(resultSet, preparedStatement, connection);
		}
		return isSuccess;
	}

	public boolean deleteById(int userId, int roleId) {  // 删除功能
		boolean isSuccess = false;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM user_role WHERE user_id=? AND role_id=?";
			preparedStatement = connection.prepareStatement(sql);
			System.out.println("删除userRole的SQL语句："+preparedStatement);
			preparedStatement.setInt(1,userId);
			preparedStatement.setInt(2,roleId);
			int count = preparedStatement.executeUpdate();
			if (count>0) {
				isSuccess = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(resultSet, preparedStatement, connection);
		}
		return isSuccess;
	}
	
}
